package com.sun.controller.admin;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.pojo.Dept;
import com.sun.pojo.Menu;
import com.sun.pojo.MenuNode;
import com.sun.service.DeptService;
import com.sun.service.MenuService;
import com.sun.utils.DataGridViewResult;
import com.sun.utils.SystemConstant;
import com.sun.utils.TreeUtil;
import com.sun.vo.DeptVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @RequestMapping("/list")
    public DataGridViewResult findDeptListByPage(DeptVo deptVo) {
        // 设置分页信息
        PageHelper.startPage(deptVo.getPage(), deptVo.getLimit());
        // 调用方法查询部门数据
        List<Dept> deptListByPage = deptService.findDeptListByPage(deptVo);
        // 创建分页对象
        PageInfo<Dept> deptPageInfo = new PageInfo<Dept>(deptListByPage);
        return new DataGridViewResult(deptPageInfo.getTotal(), deptPageInfo.getList());
    }

    @RequestMapping("/allDept")
    public DataGridViewResult allDept() {
        List<Dept> deptListByPage = deptService.findAllDept();
        return new DataGridViewResult(deptListByPage);
    }

    @RequestMapping("/addDept")
    public String addDept(Dept dept) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i = deptService.addDept(dept);
        if (i > 0) {
            map.put(SystemConstant.SUCCESS, true);
            map.put(SystemConstant.MESSAGE, dept.getDeptName() + " 添加成功!");
        } else {
            map.put(SystemConstant.SUCCESS, false);
            map.put(SystemConstant.MESSAGE, dept.getDeptName() + " 添加失败!");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/updateDept")
    public String updateDept(Dept dept) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i = deptService.updateDept(dept);
        if (i > 0) {
            map.put(SystemConstant.SUCCESS, true);
            map.put(SystemConstant.MESSAGE, dept.getDeptName() + " 修改成功!");
        } else {
            map.put(SystemConstant.SUCCESS, false);
            map.put(SystemConstant.MESSAGE, dept.getDeptName() + " 修改失败!");
        }
        return JSON.toJSONString(map);
    }

    @PostMapping("/deleteDept")
    public String deleteDept(int deptId) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i = deptService.deleteDept(deptId);
        if (i > 0) {
            map.put(SystemConstant.SUCCESS, true);
            map.put(SystemConstant.MESSAGE, "删除成功!");
        } else {
            map.put(SystemConstant.SUCCESS, false);
            map.put(SystemConstant.MESSAGE, "删除失败!");
        }
        return JSON.toJSONString(map);
    }
}
